//
//  KRTabBarViewController.m
//  cookBook
//
//  Created by kuroken on 16/1/8.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRTabBarViewController.h"
#import "KRConst.h"
#import "KRNavigationController.h"
#import "KRHomeViewController.h"
#import "KRGatherController.h"

@interface KRTabBarViewController ()

@end

@implementation KRTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    KRHomeViewController *homeVc = [[KRHomeViewController alloc] init];
    [self addChildVcWithViewController:homeVc Title:@"菜谱" image:@"menu_ico_home" highImage:@"menu_ico_home_on"];
    
    UIViewController *shopVc = [[UIViewController alloc] init];
    [self addChildVcWithViewController:shopVc Title:@"到家" image:@"menu_ico_shop" highImage:@"menu_ico_shop_on"];
    
    KRGatherController *gatherVc = [[KRGatherController alloc] init];
    [self addChildVcWithViewController:gatherVc Title:@"广场" image:@"menu_ico_gather" highImage:@"menu_ico_gather_on"];
    
    UIViewController *centerVc = [[UIViewController alloc] init];
    [self addChildVcWithViewController:centerVc Title:@"我的" image:@"menu_ico_center" highImage:@"menu_ico_center_on"];

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 去除tabbar顶部黑线
    for (UIView *subview in self.tabBar.subviews) {
        if ([subview isKindOfClass:[UIImageView class]] && subview.bounds.size.height < 1.0) {
            [subview removeFromSuperview];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addChildVcWithViewController:(UIViewController *)vc Title:(NSString *)title image:(NSString *)image highImage:(NSString *)highImage
{
    // #warning:这行代码会提前创建vc.view
//    vc.view.backgroundColor = [UIColor redColor];
    
//    vc.tabBarItem.title = title;
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:highImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = KRColor(111, 143, 18);
    [vc.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    KRNavigationController *nav = [[KRNavigationController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
}

@end
